package com.jy.modules.cims.component.common.service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.data.common.dao.TCMmCooperorgDao;
import com.jy.modules.cims.data.common.dto.TCMmCooperorgDTO;
import com.jy.platform.core.common.BaseDTO;

/**
 * @classname: TCMmCooperorgService
 * @description: 定义 合作机构 实现类
 * @author: JY-IT-D001
 */
@Service("com.jy.modules.cims.component.common.service.TCMmCooperorgService")
public class TCMmCooperorgService implements Serializable {

	private static final long serialVersionUID = 1L;

	@Autowired
	private TCMmCooperorgDao dao;

	/**
	 * @author JY-IT-D001
	 * @description: 分页查询 合作机构列表
	 * @date 2015-01-05 19:57:56
	 * @param searchParams
	 *            条件
	 * @return
	 * @throws
	 */
	public List<TCMmCooperorgDTO> searchTCMmCooperorgByPaging(Map<String, Object> searchParams) throws Exception {
		List<TCMmCooperorgDTO> dataList = dao.searchTCMmCooperorgByPaging(searchParams);
		return dataList;
	}

	/**
	 * @author JY-IT-D001
	 * @description: 按条件查询合作机构列表
	 * @date 2015-01-05 19:57:56
	 * @param searchParams
	 *            条件
	 * @return
	 * @throws
	 */
	public List<TCMmCooperorgDTO> searchTCMmCooperorg(Map<String, Object> searchParams) throws Exception {
		List<TCMmCooperorgDTO> dataList = dao.searchTCMmCooperorg(searchParams);
		return dataList;
	}

	/**
	 * @author JY-IT-D001
	 * @description: 查询合作机构对象
	 * @date 2015-01-05 19:57:56
	 * @param id
	 * @return
	 * @throws
	 */
	public TCMmCooperorgDTO queryTCMmCooperorgByPrimaryKey(String id) throws Exception {

		TCMmCooperorgDTO dto = dao.findTCMmCooperorgByPrimaryKey(id);

		if (dto == null)
			dto = new TCMmCooperorgDTO();

		return dto;

	}

	/**
	 * @title: insertTCMmCooperorg
	 * @author shengzhoutao
	 * @description: 新增 合作机构对象
	 * @date 2015-01-05 19:57:56
	 * @param dto
	 * @return
	 * @throws
	 */
	@SuppressWarnings("all")
	public Long insertTCMmCooperorg(TCMmCooperorgDTO dto) throws Exception {
		TCMmCooperorgDTO hisCooperorg = queryTCMmCooperorgByCooperorgCode(dto.getCooperorgCode());
		if (null != hisCooperorg) {
			throw new Exception(java.text.MessageFormat.format("合作机构编号[{0}]已经存在！", new Object[] { dto.getCooperorgCode() }));
		}
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);

		int count = dao.insertTCMmCooperorg(paramMap);

		TCMmCooperorgDTO resultDto = (TCMmCooperorgDTO) paramMap.get("dto");
		Long keyId = resultDto.getId();
		return keyId;
	}

	/**
	 * @title: queryTCMmCooperorgByCooperorgCode
	 * @author shengzhoutao
	 * @description: 根据合作机构编号查询合作机构信息
	 * @param cooperorgCode
	 *            合作机构编号
	 * @return TCMmCooperorgDTO
	 */
	private TCMmCooperorgDTO queryTCMmCooperorgByCooperorgCode(String cooperorgCode) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		TCMmCooperorgDTO cooperorg = new TCMmCooperorgDTO();
		cooperorg.setCooperorgCode(cooperorgCode);
		searchParams.put("dto", cooperorg);
		List<TCMmCooperorgDTO> cooperorgList = dao.searchTCMmCooperorg(searchParams);

		cooperorg = null;
		searchParams = null;
		if (CollectionsUtils.isEmpty(cooperorgList)) {
			return null;
		}

		return cooperorgList.get(0);
	}

	/**
	 * @title: updateTCMmCooperorg
	 * @author shengzhoutao
	 * @description: 修改 合作机构对象
	 * @date 2015-01-05 19:57:56
	 * @param paramMap
	 * @return
	 * @throws
	 */
	public void updateTCMmCooperorg(TCMmCooperorgDTO dto) throws Exception {
		TCMmCooperorgDTO hisCooperorg = dao.findTCMmCooperorgByPrimaryKey(String.valueOf(dto.getId()));
		if (null == hisCooperorg) {
			throw new Exception("原合作机构信息不存在！");
		}

		if (!hisCooperorg.getCooperorgCode().equals(dto.getCooperorgCode())) {
			hisCooperorg = queryTCMmCooperorgByCooperorgCode(dto.getCooperorgCode());
			if (null != hisCooperorg) {
				throw new Exception(java.text.MessageFormat.format("合作机构编号[{0}]已经存在！", new Object[] { dto.getCooperorgCode() }));
			}
		}

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);

		dao.updateTCMmCooperorg(paramMap);
	}

	/**
	 * @title: deleteTCMmCooperorgByPrimaryKey
	 * @author JY-IT-D001
	 * @description: 删除 合作机构,按主键
	 * @date 2015-01-05 19:57:56
	 * @param paramMap
	 * @throws
	 */
	public void deleteTCMmCooperorgByPrimaryKey(BaseDTO baseDto, String ids) throws Exception {
		if (StringUtils.isEmpty(ids))
			throw new Exception("删除失败！传入的参数主键为null");

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", baseDto);
		paramMap.put("ids", ids);
		dao.deleteTCMmCooperorgByPrimaryKey(paramMap);
	}

}
